package com.jsmy.softapp.fragment;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.softapp.R;
import com.jsmy.softapp.adapter.HomeFragmentAdapter;
import com.jsmy.softapp.adapter.HomeFragmentZAdapter;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.bean.AppTJListBean;
import com.jsmy.softapp.bean.AppZLListBean;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.presenter.HomePresenter;
import com.jsmy.softapp.util.ShareUtil;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.view.HomeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/17.
 * Time: 17:48.
 * To change for file | settings | file and code templates.
 */

@Route(path = AR.F_HOME)
public class HomeFragment extends BaseFragment<HomeView, HomePresenter> implements HomeView, UMShareListener {

    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_paihang)
    TextView tvPaihang;
    @BindView(R.id.m_rcycler)
    RecyclerView mRcycler;
    HomeFragmentAdapter adapter;
    List<AppTJListBean.DataBean.ListBean> list;

    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.z_rcycler)
    RecyclerView zRcycler;
    HomeFragmentZAdapter zAdapter;
    List<AppZLListBean.DataBean.ListBean> listZ;

    String shareId;
    String sharePt;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter();
    }

    @Override
    protected int setView() {
        return R.layout.fragment_home;
    }

    @Override
    protected void init(View view) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        tvTime.setText("(" + format.format(new Date()) + ")");
        tvPaihang.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        adapter = new HomeFragmentAdapter();
        mRcycler.setLayoutManager(new LinearLayoutManager(mActivity));
        mRcycler.setAdapter(adapter);

        tvMore.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        zAdapter = new HomeFragmentZAdapter();
        zRcycler.setLayoutManager(new LinearLayoutManager(mActivity));
        zRcycler.setAdapter(zAdapter);

        zAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.img_tip:
                        shareId = listZ.get(position).getId();
                        ShareUtil.shareWeb(mActivity, listZ.get(position).getUrl(), listZ.get(position).getTitle(), listZ.get(position).getNote(), HomeFragment.this);
                        break;
                    case R.id.linear_bar:
                        ARouter.getInstance().build(AR.A_SHARE)
                                .withString("id", listZ.get(position).getId())
                                .withString("url", listZ.get(position).getUrl())
                                .withString("title", listZ.get(position).getTitle())
                                .withString("note", listZ.get(position).getNote())
                                .withString("share", "Y")
                                .navigation();
                        break;
                }
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.getsyappzllist();
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        presenter.getapptjlist();
        presenter.getsyappzllist();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        refreshLayout.finishRefresh();
    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type) {
            case API.APP_TJ_LIST:
                list = ((AppTJListBean) object).getData().getList();
                adapter.replaceData(list);
                break;
            case API.APP_ZL_LIST:
                listZ = ((AppZLListBean) object).getData().getList();
                zAdapter.replaceData(listZ);
                break;
            case API.APP_SAVE_FX_INFO:
                ToastUtil.showShort(mActivity, "分享成功！");
                break;
        }
    }

    @Override
    public void onDestroyView() {
        presenter.model.disposable.clear();
        super.onDestroyView();
    }

    @OnClick({R.id.tv_paihang, R.id.tv_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_paihang:
                ARouter.getInstance().build(AR.A_RANKLIST).navigation();
                break;
            case R.id.tv_more:
                ARouter.getInstance().build(AR.A_DATA).navigation();
                break;
        }
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                sharePt = "QQ";
                break;
            case QZONE:
                sharePt = "QZONE";
                break;
            case SINA:
                sharePt = "SINA";
                break;
            case WEIXIN:
                sharePt = "WEIXIN";
                break;
            case WEIXIN_CIRCLE:
                sharePt = "WEIXIN_CIRCLE";
                break;
            case SMS:
                sharePt = "SMS";
                break;
            case EMAIL:
                sharePt = "EMAIL";
                break;
        }
        presenter.savefxinfo(shareId, "", sharePt, "");
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.showShort(mActivity, "分享失败，" + throwable.getMessage());
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.showShort(mActivity, "分享取消");
    }

    @Override
    public void updata() {
        presenter.getapptjlist();
    }

}
